	var stopSourceArr = [],stopDetailArr = [],stopFromDic;
	var sliderIndex = 0;
	var executeSuc = 0,executeFail = 0,allCount;
	var isAllFinish = true;
	mui.init();
	mui.plusReady(function() {
		hideAllBtn();
		perInfo = app.getState()
		basicSetup();
	});

	function segmentGroupUpdate(index) {
		var segmentGroup = document.getElementById('segmentGroup');
		segmentGroup.innerHTML = '';
		var sliderGroup = document.getElementById('sliderGroup');
		sliderGroup.innerHTML = '';
		var segmentHtml = '';
		var sliderHtml = '';
		for(var i = 0; i < stopSourceArr.length; i++) {
			var oneDict = stopSourceArr[i];
			var stopDate = oneDict['DateTimeStr'];
			if(oneDict['SASAStatus'] == "已审核") {
				isAllFinish = false;
			}
			stopDate = stopDate.substring(5, stopDate.length);
			var stopTime = oneDict['StopTimeRange'];
			var destStr = stopDate + '<br/>' + stopTime;
			var active = ''
			if (i == index) {
				active = 'mui-active'
			}
			segmentHtml += '<a id="segment-'+i+'" class="mui-control-item '+active+'" href="#item-'+i+'">'+
					'<div class="segmentTitle">'+destStr+'</div>'+
					'<div class="segmentTitleLine"></div></a>';
			sliderHtml += '<div id="item-'+i+'" class="mui-slider-item mui-control-content '+active+'">'+
					'<div class="mui-scroll-wrapper"><div class="mui-scroll mui-scroll-iPhone">'+
					'<ol id="tableVeiw'+i+'" class="mui-table-view"></ol></div></div></div>';
		}
		sliderGroup.innerHTML = sliderHtml;
		mui('.mui-scroll-wrapper').scroll();
		sliderIndex = index
		if(stopSourceArr.length == 1) {
			segmentGroup.style.display = 'none'
		} else {
			segmentGroup.innerHTML = segmentHtml;
			var slider = document.getElementById('slider')
			slider.addEventListener('slide', function(e) {
				plus.nativeUI.showWaiting()
				setTimeout(function() {
					plus.nativeUI.closeWaiting()
					var index = e.detail.slideNumber;
					if(stopDetailArr[index] == null) {
						//获取详情 
						getStopDetailData(index, 0);
					}
					sliderIndex = index;
					showDealBtn();
				}, 300);
			});
		}
		executeSuc = 0;
		executeFail = 0;
		for(var i = 0; i < stopSourceArr.length; i++) {
			getStopDetailData(i, 1);
		}
		mui('.mui-slider').slider();
		
		if(isAllFinish) {
			hideAllBtn();
		} else {
			showDealBtn();
		}
	}
	/*
	 * typeIndex:0只请求一个 1 第一次获取  2信息更新 3 执行一个后刷新一个
	 */
	function getStopDetailData(index, typeIndex) {
		plus.nativeUI.showWaiting('正在加载');
		var pdict = {'parr':stopSourceArr[index]['RBASId']}
		var paramDict = {'method':'web.INMOutInterface:getSASAInfo',
		'params':JSON.stringify(pdict),'type':'RecMethod'}
		//console.log(JSON.stringify(pdict))
		mui.DHCWebService(paramDict, function(result) {
			plus.nativeUI.closeWaiting();
			//console.log(JSON.stringify(result))
			var data = result.data
			if (result.msgcode != '1') {
				updateTableviewUI(index,null,result.msg);
				executeFail += 1;
			}else if (!data) {
				updateTableviewUI(index,null,'没有data字段值');
				executeFail += 1;
			}else {
				stopDetailArr[index] = data;
				updateTableviewUI(index,data);
				executeSuc += 1;
			}
			if(executeFail + executeSuc == allCount) {
				plus.nativeUI.closeWaiting();
			}
		}, function(error) {
			executeFail += 1;
			plus.nativeUI.closeWaiting();
			mui.toast(error);
			updateTableviewUI(index,null);
			if(executeFail + executeSuc == allCount) {
				plus.nativeUI.closeWaiting();
			}
		});
	}

	function updateTableviewUI(index,detailData,msg) {
		if(detailData == null) { //提示没有详情数据
			mui('#tableVeiw'+index)[0].innerHTML = '<div class="nullText">'+(msg?msg:'获取停诊信息失败')+'</div>';
			return;
		}
		var tableVeiw = mui('#tableVeiw'+index)[0];
		var stopStatus = detailData['TStatusDesc']; //状态TStatusDesc
		var dict = stopSourceArr[index]
		//console.log(index);
		var stopReason = dict['StopReason']; //停诊原因
		var locDesc = detailData['DepName']; //科室
		var docName = detailData['DoctorName']; //医生名
		var stopTime = detailData['SessionTimeName']; //停诊时段 上午/下午
		var stopDate = detailData['AsDate']; //停诊日期
		var chineseDateStr = stopDate.replace('-','年').replace('-','月') + '日'+stopTime
		var ghaoType = detailData['SessionTypeDesc']; //职称
		var applyDate = detailData['TUpdateDate']; //申请日期
		var applyTime = detailData['TUpdateTime']; //申请时间
		var yuyueNo = detailData['AppedNum']||''; //预约人数
		var guahaoNo = detailData['RegisterNum']||''; //挂号人数
		var quhaoNo = detailData['AppedArriveNum']||''; //预约人数
		var RBASStopFlag = dict['RBASStopFlag'];
		var SASAStatus = dict['SASAStatus'];
		//编辑短信按钮
		var msgBtnHtml = '';
		// console.log('index',index)
		if(SASAStatus == '已审核') {
			msgBtnHtml = '<span id="editMsg-' + index + '" class="mui-badge edit">编辑短信</span>'
		}

		//停诊处理 通知患者退号,请XX医生替诊 补诊时间
		var stopResult = dict['StopResult'];
		var stopResultTitle = dict['stopResultTitle']||'';
		while(stopResultTitle.indexOf('+') >= 0) {
			stopResultTitle = stopResultTitle.replace('++', ' ');
			stopResultTitle = stopResultTitle.replace('+', ' ');
		}
		if(stopResultTitle == '') {
			stopResultTitle = stopResult;
		}
		var stopResultTitleTip = '停诊处理';
		var replaceGhaoType = '';
		var msgStr = '尊敬的患者，您预约的'+chineseDateStr+locDesc+'，' + docName + '医生的门诊，因故停诊。' ;
		
		if(stopResult == '替诊') {
			stopResultTitleTip = '替诊医生';
			stopResultTitle = dict['ERepDoc']||'';
			msgStr += '已为您安排相同时间的其他医生接诊，请到就诊科室护士站办理换号。如您不同意医院安排，可以'
		} else if(stopResult == '补诊') {
			stopResultTitleTip = '补诊日期';
			stopResultTitle = (dict['SuppleASDate']||'') +' ' + (dict['SuppleTimeRangeDesc']||'');
			var msgTip = (dict['SuppleASDate'].replace('-','年').replace('-','月')) +'日' + (dict['SuppleTimeRangeDesc']||'')
			msgStr += '已为您安排'+msgTip+'就诊，请注意查收就诊短信通知，或尽快';
		} else {
			msgStr += '您可以';
		}
		msgStr += '通过原挂号途径或医院窗口办理退号。给您就诊带来的不便，敬请谅解，咨询电话（010）69151188。'
		if (detailData['message']) {
			msgStr = detailData['message']
		}
		var msgHtml = '<li class="mui-table-view-cell mui-disabled">' +
			'<a>' + msgBtnHtml + '退号短信</a>' +
			'<a id="msgContent-'+index+'" class="mui-left" style="padding-left: 20px;">' + msgStr + '</a></li>';
		// if(stopResult == '替诊') {
		// 	msgHtml = '';
		// }
		var roomName = detailData['RoomName']; //诊室	
		//分时段就诊 'Y^27900^35100^30^4^1-1,2-2,3-3,4-4^07:45-08:15,08:15-08:45,08:45-09:15,09:15-09:45^'
		var trInfo = detailData['TRInfo'];
		var trInfoHtml = '<li class="mui-table-view-cell mui-disabled">' +
			'<a><input class="mui-badge" type="checkbox" onclick="return false;" >分时段就诊</a></li>';
		if(trInfo != '') {
			var trInfoArr = trInfo.split('!');
			var trInfoStatus = trInfoArr[0];
			if(trInfoStatus == 'Y') {
				var startSeconds = trInfoArr[1]||''; //开始时间
				var startTimeStr = secondStrChangeTimeStr(startSeconds);
				var endSeconds = trInfoArr[2]||''; //截止时间
				var endTimeStr = secondStrChangeTimeStr(endSeconds);
				var spMinute = trInfoArr[3]||''; //间隔分数
				//时段号数
				var spWidth = window.screen.width - 100;
				var spTimeArr = (trInfoArr[6]||'').split(',');
				var spNumArr = (trInfoArr[5]||'').split(',');
				var spnumberStr = '';
				for(var i = 0; i < spTimeArr.length; i++) {
					var time = spTimeArr[i];
					var num = spNumArr[i];
					var html = time + '&nbsp;[&nbsp;<span style="color:gray">' + num + '</span>&nbsp;]<br />';
					spnumberStr += html;
				}
				trInfoHtml = '<li class="mui-table-view-cell mui-disabled">' +
					'<a><input class="mui-badge" type="checkbox" checked="checked" onclick="return false;" >' +
					'分时段就诊</a></li>' +
					'<li class="mui-table-view-cell mui-disabled">' +
					'<a><span class="mui-badge">' + startTimeStr + '</span>开始时间</a></li>' +
					'<li class="mui-table-view-cell mui-disabled">' +
					'<a><span class="mui-badge">' + endTimeStr + '</span>截止时间</a></li>' +
					'<li class="mui-table-view-cell mui-disabled">' +
					'<a><span class="mui-badge">' + spMinute + '</span>间隔分钟</a></li>' +
					'<li class="mui-table-view-cell mui-disabled">' +
					'<a id="spNumbera-' + index + '"><span id="spNumber-' + index + '" class="mui-badge" style="width:' + spWidth + 'px;text-align:right;">' + spnumberStr + '</span>时段号数</a></li>';
			}
		}

		tableVeiw.innerHTML =
			'<li class="mui-table-view-cell mui-disabled">' +
			'<a><span class="mui-badge">' + stopStatus + '</span>状态</a></li>' +
			'<li class="mui-table-view-cell mui-disabled">' +
			'<a><span class="mui-badge">' + docName + '&nbsp;<span class="ghaoType">[' +
			ghaoType + ']</span></span>医生</a></li>' +
			'<li class="mui-table-view-cell mui-disabled">' +
			'<a><span class="mui-badge">' + stopDate + '&nbsp;' + stopTime + '</span>出诊日期</a></li>' +
			'<li class="mui-table-view-cell mui-disabled">' +
			'<a><span class="mui-badge">' + stopResultTitle + '</span>' + stopResultTitleTip + '</a></li>' +
			replaceGhaoType +
			'<li class="mui-table-view-cell mui-disabled">' +
			'<a><span class="mui-badge">' + stopReason + '</span>停诊原因</a></li>' +
			'<li class="mui-table-view-cell mui-disabled">' +
			'<a><span class="mui-badge">' + locDesc + '</span>科室</a></li>' +
			'<li class="mui-table-view-cell mui-disabled">' +
			'<a><span class="mui-badge">' + roomName + '</span>诊室</a></li>' +
			'<li class="mui-table-view-cell mui-disabled">' +
			'<a><span class="mui-badge">' + yuyueNo + '</span>已预约数</a></li>' +
			'<li class="mui-table-view-cell mui-disabled">' +
			'<a><span class="mui-badge">' + guahaoNo + '</span>已挂号数</a></li>' +
			'<li class="mui-table-view-cell mui-disabled">' +
			'<a><span class="mui-badge">' + quhaoNo + '</span>已取号数</a></li>' +
			'<li class="mui-table-view-cell mui-disabled">' +
			'<a><span class="mui-badge">' + applyDate + '</span>创建日期</a></li>' +
			'<li class="mui-table-view-cell mui-disabled">' +
			'<a><span class="mui-badge">' + applyTime + '</span>创建时间</a></li>' +
			trInfoHtml + msgHtml;
		//调整分时段就诊的高度
		var spNumbera = document.getElementById('spNumbera-' + index);
		var spNumber = document.getElementById('spNumber-' + index);
		if(spNumbera) {
			spNumbera.style.height = spNumber.clientHeight + 10 + 'px';
		}
		var editMsg = document.getElementById('editMsg-' + index);
		if(editMsg) {
			editMsg.addEventListener('tap', editMsgClicked, false);
		}
		// var replaceGhaoType = document.getElementById('replaceG-' + index);
		// if(replaceGhaoType) {
		// 	replaceGhaoType.addEventListener('tap', replaceGhaoTypeClicked, false);
		// }
	}

	/*
	 * 发生停诊(补诊)/替诊
	 * 执行
	 */
	function dealOneBtnClicked() {
		var btnTitle = this.innerText;
		if(btnTitle == '执行完成') {
			titleArr = ['点错了', '执行完成'];
			mui.confirm('确认执行完成吗?', '温馨提示', titleArr, function(e) {
				if(e.index == 1) {
					cancelOrExecuteOne('E', sliderIndex, false);
				}
			});
			return;
		} else if(btnTitle == '发生替诊') { //发生停诊
			var replaceGCE = document.getElementById('replaceGC-' + sliderIndex);
			var replaceGhoaID = replaceGCE.getAttribute('data-typeID');
			if(replaceGhoaID == null || replaceGhoaID == '') {
				mui.toast('请选择替诊职称');
				return;
			}
		}
	}
	function dealAllBtnClicked() {
		var btnTitle = this.innerText;
		if(btnTitle.indexOf('替诊') >= 0) {
			for(var i = 0; i < stopSourceArr.length; i++) {
				var replaceGCE = document.getElementById('replaceGC-' + i);
				var replaceGhoaID = replaceGCE.getAttribute('data-typeID');
				if(replaceGhoaID == null || replaceGhoaID == '') {
					mui.toast('请选择替诊职称');
					
					return;
				}
			}
		}
		var msgStr = '确认' + btnTitle + '吗?';
		var titleArr = ['取消', '确定'];
		mui.confirm(msgStr, '温馨提示', titleArr, function(e) {
			if(e.index == 1) {
				executeSuc = 0;
				executeFail = 0;
				cancelOrExecuteAll('E')
				// for(var i = 0; i < stopSourceArr.length; i++) {
				// 	getStopReasonId(i, true);
				// }
			}
		});
	}
	function cancelBtnClicked(item) {
		var btnTitle = item.innerText;
		var titleStr = '确认' + item.innerText + '吗?';
		var btnArr = ['点错了', btnTitle];
		// if(stopSourceArr.length > 1 && btnTitle.indexOf('撤销') != -1) {
		// 	btnArr = ['点错了', btnTitle + '本条', '全部' + btnTitle];
		// }
		mui.confirm(titleStr, '温馨提示', btnArr, function(e) {
			if(e.index == 1) {
				cancelOrExecuteOne('CE', sliderIndex, false);
			} else if(e.index == 2) {
				cancelOrExecuteAll('CE');
			}
		});
	}
	/*
	 * 执行完成一条/取消执行一条/未做任何操作直接取消执行
	 * 执行完成一条80143 '<Request>'+SASAId+'^E</Request>'
	 * 取消执行一条80143 '<Request>'+SASAId+'^CE</Request>'
	 * 撤销停诊80142'<Request>'+ScheduleID+'</Request>'
	 */
	function cancelOrExecuteOne(typeStr, index, isAll) {
		console.log(typeStr,index,isAll)
		var msgContent = document.getElementById('msgContent-' + index).innerText; //短信的内容
		var uid = perInfo.hisUserID
		var waittingStr = '正在执行';
		if(typeStr == 'CE') {
			msgContent = ''
			waittingStr = '正在取消执行';
		}
		var xmlStr = stopSourceArr[index]['SASAId'] + '^' + typeStr + '^' + uid + '^' + msgContent + '^' + stopSourceArr[index]['StopTimeRange']+ '^' + stopSourceArr[index]['RBASId'];
		var pdict = {'parr':xmlStr}
		// 执行入参
		console.log(JSON.stringify(pdict));
		var paramDict = {'method':'web.INMOutInterface:UpDateStopAS',
			'params':JSON.stringify(pdict),'type':'Method'}
		
		if(!isAll || index == 0) {
			plus.nativeUI.showWaiting(waittingStr);
		}
		mui.DHCWebService(paramDict, function(result) {
			if(!isAll) {
				if (result.msgcode != '1' || result.msg.indexOf('成功') == -1) {
					if(waittingStr == '正在执行') {
						mui.toast('执行失败');
					} else {
						mui.toast('取消执行失败');
					}
				}else{
					if(waittingStr == '正在执行') {
						mui.toast('执行成功');
					} else {
						mui.toast('取消执行成功');
					}
					getStopListData(); //刷新界面
				}
				plus.nativeUI.closeWaiting();
			} else {
				if (result.msgcode != '1' || result.msg.indexOf('成功') == -1) {
					executeFail += 1;;
				} else {
					executeSuc += 1;
				}
				executeToast();
			}
		//}, 1000);
		}, function(error) {
			mui.toast(error);
			if(isAll) {
				executeFail += 1;
				executeToast();
			} else {
				plus.nativeUI.closeWaiting();
			}
		});
	}

	function cancelOrExecuteAll(type) {
		executeSuc = 0;
		executeFail = 0;
		plus.nativeUI.showWaiting('正在执行');
		for(var i = 0; i < stopSourceArr.length; i++) {
			if(stopSourceArr[i]['SASAStatus'] === '已审核'){
				cancelOrExecuteOne(type, i, true);
			}
		}
	}

	function basicSetup() {
		var self = plus.webview.currentWebview();
		stopFromDic = self.stopData;
		stopResult = stopFromDic['StopResult'];
		stopSourceArr = self.stopSourceArr;
		dealOneBtn.addEventListener('tap', dealOneBtnClicked);
		dealAllBtn.addEventListener('tap', dealAllBtnClicked);

		var cancelBtn = document.getElementById('cancelBtn');
		cancelBtn.addEventListener('tap', function() {
			cancelBtnClicked(this);
		});
		segmentGroupUpdate(self.stopDataIndex);
	}

	function hideAllBtn() {
		var dealOneBtn = document.getElementById('dealOneBtn');
		var dealAllBtn = document.getElementById('dealAllBtn');
		var slider = document.getElementById('slider');
		slider.style.bottom = '0px';
		dealOneBtn.style.display = 'none';
		dealAllBtn.style.display = 'none';
		var cancelBtn = document.getElementById('cancelBtn');
		cancelBtn.style.display = 'none';
	}

	/*
	 * dealOneBtn dealAllBtn cancelBtn的显示
	 */
	function showDealBtn() {
		var dealOneBtn = document.getElementById('dealOneBtn');
		var dealAllBtn = document.getElementById('dealAllBtn');
		var cancelBtn = document.getElementById('cancelBtn');
		if(isAllFinish || stopSourceArr.length == 0) {
			dealOneBtn.style.display = 'none';
			dealAllBtn.style.display = 'none';
			cancelBtn.style.display = 'none';
			return;
		}
		var slider = document.getElementById('slider');
		slider.style.bottom = '50px';
		var oneDict = stopSourceArr[sliderIndex];
		if(oneDict['SASAStatus'] == "已审核") {
			cancelBtn.style.display = 'block';
			if(stopSourceArr.length == 1 ){//&& oneDict['RBASStopFlag'] == '0') {
				dealAllBtn.style.display = 'none';
				dealOneBtn.style.display = 'block';
				dealOneBtn.style.borderBottom = '0px';
				dealOneBtn.style.width = '100%';
				return;
			}
			dealAllBtn.style.display = 'block';
			dealOneBtn.style.display = 'block';
			// if(oneDict['RBASStopFlag'] == '0') { //发生停诊/全部停诊
			// 	dealOneBtn.style.display = 'block';
			// 	dealAllBtn.style.display = 'block';
			// 	dealOneBtn.style.width = '50%';
			// 	dealOneBtn.style.borderBottom = '0.5px lightgray solid';
			// 	cancelBtn.innerText = '取消执行';
			// } else { //执行/取消执行
			// 	dealOneBtn.innerText = '执行完成';
			// 	dealAllBtn.style.display = 'none';
			// 	dealOneBtn.style.display = 'block';
			// 	dealOneBtn.style.width = '100%';
			// 	dealOneBtn.style.borderBottom = '0px';
			// 	if(stopResult == '替诊') {
			// 		cancelBtn.innerText = '撤销替诊';
			// 	} else {
			// 		cancelBtn.innerText = '撤销停诊';
			// 	}
			// }
		} else {
			dealOneBtn.style.display = 'none';
			dealAllBtn.style.display = 'none';
			cancelBtn.style.display = 'none';
		}
	}

	/*
	 * 编辑短信
	 */
	function editMsgClicked() {
		var msgE = document.getElementById('msgContent-' + sliderIndex);
		mui.openWindow({
			url: 'nurseStopEditMsg.html',
			id: 'nurseStopEditMsg',
			extras: {
				oldMsg: msgE.innerText,
			}
		});
	}

	/*
	 * 更新原始数据,重新填充界面
	 */
	function getStopListData() {
		plus.nativeUI.showWaiting('信息更新');
		var self = plus.webview.currentWebview();
		var paramDict = self.listXmlStr;
		console.log(JSON.stringify(paramDict))
		mui.DHCWebService(paramDict, function(result) {
			plus.nativeUI.closeWaiting();
			if (result.msgcode != '1') {
				mui.toast(result.msg)
				return;
			}
			var resultArray = result.data
			stopSourceArr.splice(0, stopSourceArr.length);
			for(var i = 0; i < resultArray.length; i++) {
				var oneDict = resultArray[i];
				if(oneDict['ONCERequestID'] == stopFromDic['ONCERequestID']) {
					stopSourceArr.push(oneDict);
				}
			}
			stopSourceArr = jsonSort(stopSourceArr,'SASAId') // 按照这个字段对上下午进行排个序
			console.log(JSON.stringify(stopSourceArr));
			console.log(stopSourceArr.length);
			stopFromDic = stopSourceArr[sliderIndex];
			updateSourceData();
		}, function(error) {
			plus.nativeUI.closeWaiting();
		});
	}

	function updateSourceData() {
		console.log(stopSourceArr.length)
		for(var i = 0; i < stopSourceArr.length; i++) {
			var oneDict = stopSourceArr[i];
			if(oneDict['SASAStatus'] == "已审核") {
				isAllFinish = false;
				break;
			}
		}
		console.log(isAllFinish)
		// return
		var dealOneBtn = document.getElementById('dealOneBtn');
		var dealAllBtn = document.getElementById('dealAllBtn');
		if(isAllFinish) {
			var slider = document.getElementById('slider');
			slider.style.bottom = '0px';
			dealOneBtn.style.display = 'none';
			dealAllBtn.style.display = 'none';
			return;
		}
		showDealBtn();
		executeSuc = 0;
		executeFail = 0;
	}

	function executeToast() {
		console.log('成功：',executeSuc + '条')
		console.log('失败：',executeFail + '条')
		plus.nativeUI.closeWaiting()
		var toastStr = '成功：' + executeSuc + ' 条';
		if(executeFail != 0){
			toastStr += '\n失败：' + executeFail + ' 条'
		}
		mui.toast(toastStr);
		//刷新界面
		getStopListData();
	}


/*
 * 更新本页短信内容
 */
function msgEditFinish(msgStr) {
	var msgE = document.getElementById('msgContent-' + sliderIndex);
	msgE.innerText = msgStr;
}
